package com.restaurant.serviceimpl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.impl.CriteriaImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.restaurant.domain.Employeecredential;
import com.restaurant.service.EmployeecredentialService;

public class EmployeecredentialServiceImpl implements EmployeecredentialService {

   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory){
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employeecredential addOrModify( Employeecredential objEmployeecredential ) {
       return ( Employeecredential ) hibernateTemplate.merge( objEmployeecredential );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employeecredential deleteRecord( Employeecredential objEmployeecredential ) {
       hibernateTemplate.delete( objEmployeecredential );
       return objEmployeecredential;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employeecredential modifyRecord( Employeecredential objEmployeecredential ) {
       return ( Employeecredential ) hibernateTemplate.merge( objEmployeecredential );
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Employeecredential> loadAll() {
       return (List<Employeecredential>)hibernateTemplate.findByNamedQuery( "Employeecredential.findAll");
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employeecredential loadByPrimaryKey( Long primaryKey ) {
       return (Employeecredential) hibernateTemplate.get( Employeecredential.class, primaryKey );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employeecredential deleteByPrimaryKey( Long primaryKey ) {
       return deleteRecord(loadByPrimaryKey(primaryKey));
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Employeecredential> loadByHql(String query) {
       return ( List<Employeecredential> ) hibernateTemplate.find( query );
   }

	@Override
	public Employeecredential findByName(final String name) throws Exception {
		return (Employeecredential) hibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				session.get(Employeecredential.class, null);
				session.load(Employeecredential.class, null);
				return session.createCriteria(Employeecredential.class).add(Restrictions.eq("username", name)).uniqueResult();				
			}
		});
		
	}
}